﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using BestLifePOS_Core.Data.Commands;
using BestLifePOS_Core.DomainObjects;
using BestLifePOS_Core.Data.Fascade;
using System.Linq;

namespace BestLifePOS_PresentationLayer
{
    public partial class frmVendorList : Form
    {
        public frmVendorList()
        {
            InitializeComponent();
        }

        private void panel1_Paint(object sender, PaintEventArgs e)
        {

        }

        private void frmVendorList_Load(object sender, EventArgs e)
        {

        }

        private void btnVendorList_VNew_Click(object sender, EventArgs e)
        {
            txtVendorList_VId.Text = SupplierDAO.GetNewVendorId().ToString();
        }

        private int SaveNewVendor()
        {

            Supplier LvSup = CollectNewVendorFigures();
            if (LvSup != null)
            {
                return SupplierDAO.SaveNewSupplier(LvSup);
            }
            else
            {
                return 0;
            }
        }

        private Supplier CollectNewVendorFigures()
        {

            try
            {

                Supplier LvSup = new Supplier();


                LvSup.Sup_ID = txtVendorList_VId.Text.ToString();
                LvSup.Contact = txtVendorList_VContactPerson.Text.ToString();
                LvSup.Check_Person = txtVendorList_VAC.Text.ToString();
                LvSup.Check_Type = cmbVendorList_VPayType.Text.ToString();
                LvSup.Sup_Name = txtVendorList_VName.Text.ToString();
                LvSup.Sup_Tel = txtVendorList_VPhoneNo.Text.ToString();
                LvSup.Sup_mob = txtVendorList_VCMob.Text.ToString();
                LvSup.Sup_fax = txtVendorList_Vfax.Text.ToString();
                LvSup.Sup_email = txtVendorList_VEmail.Text.ToString();
                LvSup.Sup_web = txtVendorList_VWeb.Text.ToString();
                LvSup.Sup_Add = rtxtVendorList_VAddress.Text;
                LvSup.User_Id = "1";
                LvSup.Date_Create = DateTime.Now;
                LvSup.Last_Modify = DateTime.Now;
                LvSup.User_Modify = "1";


                return LvSup;
            }
            catch (Exception)
            {
                return null;
            }
        }

        private void btnVendorList_VSave_Click(object sender, EventArgs e)
        {
            SaveNewVendor();
            MessageBox.Show("NEW SUPPLIER WAS BSUCCESFULLY SAVED");
        }

        private void btnVendorList_SearchBill_Click(object sender, EventArgs e)
        {
            VendorFillGrid();
        }

        private void VendorFillGrid()
        {
            //Fill the DataGrid
            List<Supplier> LvList = SupplierDAO.GetAllSupplier();
            DataTable LvTable = new DataTable();
            //dgvGRN.DataSource = LvTable3;

            dgvVendorList.DataSource = LvTable;
            LvTable.Columns.Add("Sup_ID ");
            LvTable.Columns.Add("Sup_Name ");
            LvTable.Columns.Add("Contact_Person ");
            LvTable.Columns.Add("Sup_Tel ");
            LvTable.Columns.Add("Sup_mob ");
            LvTable.Columns.Add("Sup_fax ");


            foreach (Supplier LvSupplier in LvList)
            {
                LvTable = (DataTable)dgvVendorList.DataSource;

                LvTable.Rows.Add(LvTable.NewRow());

                LvTable.Rows[LvTable.Rows.Count - 1][0] = LvSupplier.Sup_ID.ToString();
                LvTable.Rows[LvTable.Rows.Count - 1][1] = LvSupplier.Sup_Name.ToString();
                LvTable.Rows[LvTable.Rows.Count - 1][2] = LvSupplier.Contact.ToString();
                LvTable.Rows[LvTable.Rows.Count - 1][3] = LvSupplier.Sup_Tel.ToString();
                LvTable.Rows[LvTable.Rows.Count - 1][4] = LvSupplier.Sup_mob.ToString();
                LvTable.Rows[LvTable.Rows.Count - 1][5] = LvSupplier.Sup_fax.ToString();


            }

        }

        private void btnVendorList_VNew_Click_1(object sender, EventArgs e)
        {
            txtVendorList_VId.Text = SupplierDAO.GetNewVendorId().ToString();
        }

        private void btnVendorList_VSave_Click_1(object sender, EventArgs e)
        {
            SaveNewVendor();
            MessageBox.Show("NEW SUPPLIER WAS BSUCCESFULLY SAVED");
        }

        private void btnVendorList_SearchBill_Click_1(object sender, EventArgs e)
        {
            VendorFillGrid();
        }
    }
}
